/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onurkaganaldemir.ktoastlib;

import ohos.agp.components.element.Element;
import ohos.agp.render.ColorMatrix;
import ohos.agp.utils.Color;

import com.chinasoft_ohos.commontools.util.NumCalcUtil;

/**
 * element颜色修改工具类
 *
 * @since 2021-05-21
 */
public class ElementTintUtil {
    /**
     * 更改图片颜色
     *
     * @param element element
     * @param maskColor 颜色值
     */
    public static void setColorTint(Element element, int maskColor) {
        // 忽略透明度时的颜色矩阵
        float first = Color.alpha(maskColor) / 255f;
        first = NumCalcUtil.sub(first, NumCalcUtil.mul(NumCalcUtil.sub(1, first),0.15f));
        float second = NumCalcUtil.mul(NumCalcUtil.sub(1, first), 1.15f);
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setMatrix(new float[]{
            second, 0, 0, 0, ((maskColor >> 16) & 0xFF) * first,
            0, second, 0, 0, ((maskColor >> 8) & 0xFF) * first,
            0, 0, second, 0, (maskColor & 0xFF) * first,
            0, 0, 0, 1, 0,
        });
        element.setColorMatrix(colorMatrix);
    }
}
